package client.view;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.RemoteException;
import java.util.Iterator;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import server.model.meeting.User;
import server.model.remotes.IMeetingService;


public class UserInfo extends JPanel {

	private GuiManager manager;
	private JLabel lblUserInfo;
	private JLabel lblName;
	private JLabel lblRepeatPassword;
	private JLabel lblPassword;
	private JLabel lblEmail;

	private JTextField txtName;
	private JLabel lblFeedback;
	private JTextField txtRepeatPassword;
	private JTextField txtPassword;
	private JTextField txtEmail;

	private JButton btnBack;
	private JButton btnSaveUserInfo;
	
	private Iterator<User> users;

	public UserInfo(GuiManager manager) {
		this.manager = manager;
		setSize(585, 365);
		setLocation(0, 0);
		setVisible(true);
		setLayout(null);

		initGUI();
		
		try {
			users = manager.getAdmin().getUsers().iterator();
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println(users);
	}
	

	private void initGUI() {
		
		lblUserInfo = new JLabel("Hieronder ziet U uw gegevens. \nKlik op een tekstvak om gegevens te wijzigen.");

		lblUserInfo.setBounds(12, 0, 300, 60);
		lblUserInfo.setFont(new java.awt.Font("Segoe UI", 1, 14));
		lblName = new JLabel("Naam :");
		lblName.setBounds(19, 63, 120, 16);
		lblEmail = new JLabel("Email :");
		lblEmail.setBounds(19, 91, 120, 16);
		lblPassword = new JLabel("Wachtwoord:");
		lblPassword.setBounds(19, 119, 120, 16);
		lblRepeatPassword = new JLabel("Herhaal wachtwoord :");
		lblRepeatPassword.setBounds(19, 147, 120, 16);
		lblFeedback = new JLabel("");
		lblFeedback.setBounds(19, 178, 248, 15);
		
		
		txtName = new JTextField();
		txtName.setBounds(167, 60, 100, 22);
		txtEmail = new JTextField();
		txtEmail.setBounds(167, 88, 100, 22);
		txtPassword = new JPasswordField();
		txtPassword.setBounds(167, 116, 100, 22);
		txtRepeatPassword = new JPasswordField();
		txtRepeatPassword.setBounds(167, 144, 100, 22);

		btnSaveUserInfo = new JButton("Opslaan");
		btnSaveUserInfo.setBounds(167, 210, 100, 22);
		btnBack = new JButton("Terug");
		btnBack.setBounds(19, 210, 100, 22);
		btnBack.addActionListener(new btnBack());

		add(lblUserInfo);
		add(lblName);
		add(lblEmail);
		add(lblPassword);
		add(lblRepeatPassword);

		add(txtName);
		add(txtEmail);
		add(txtPassword);
		add(txtRepeatPassword);
		add(lblFeedback);

		add(btnSaveUserInfo);
		add(btnBack);


		repaint();
		

	}
	
	private class btnSaveUserInfo implements ActionListener{

		public void actionPerformed(ActionEvent arg0) {
			if (txtName.getText().matches("")){
				lblFeedback.setText("Username is empty");
				return;
			}
			if (txtEmail.getText().matches("")){
				lblFeedback.setText("Email is empty");
				return;
			}
				
			
			char[] chars = ((JPasswordField) txtPassword).getPassword();
			char[] charsRepeat = ((JPasswordField) txtRepeatPassword).getPassword();
			
			if (chars.length == 0){
				lblFeedback.setText("Password is empty");
				return;
			}
			
			if (chars != charsRepeat){
				lblFeedback.setText("Het wachtwoord komt niet overeen");
			}
			
			String password = "";
			for (int i=0; i<chars.length; i++){
				password+=chars[i];
			}
			
			/** 
			 * Nog niet geimplementeerd
			try {
				if (manager.getAdmin().registerUser(txtName.getText(), password, txtEmail.getText())){
					manager.switchScreen("loginGui");
				}else{
					
				}
			
			} catch (RemoteException e) {
				e.printStackTrace();
			}
			**/
		}
		
	}
	
	public class btnBack implements ActionListener{

		public void actionPerformed(ActionEvent arg0) {
			manager.switchScreen("menuGui");
		}
		
	}

	public void reset() {
		lblFeedback.setText("");
	}

}
